﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twodi.Aspect.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Twodi.Interfaces;
using Twodi.Resource;

namespace Twodi.Aspect.Implemented
{
    /// <summary>
    /// Define um componente que possui textura.
    /// </summary>
    public class TextureAspect : AbstractTextureAspect, IName
    {
        /// <summary>
        /// Textura do componente.
        /// </summary>
        protected Texture2D texture;

        /// <summary>
        /// Nome da textura.
        /// </summary>
        protected String name;

        /// <summary>
        /// Construtor utilizando uma nova textura.
        /// </summary>
        /// <param name="texture">Textura para o aspecto.</param>
        public TextureAspect(Texture2D texture)
            : base()
        {
            this.texture = texture;
        }

        /// <summary>
        /// Construtor utilizando uma textura carregada no Resources.
        /// </summary>
        /// <param name="texture">Nome da textura.</param>
        public TextureAspect(String textureName)
            : base()
        {
            this.name = textureName;
        }

        /// <summary>
        /// Textura do componente.
        /// </summary>
        public override Texture2D Texture
        {
            get
            {
                if (Name != null && Name != String.Empty)
                {
                    return Resources.Get<Texture2D>(Name);
                }
                else
                {
                    return this.texture;
                }
            }
            set
            {
                this.texture = value;
                onTextureChanged(EventArgs.Empty);
            }
        }

        /// <summary>
        /// Nome da textura.
        /// </summary>
        public String Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
                onNameChanged(EventArgs.Empty);
            }
        }

        public event EventHandler<EventArgs> NameChanged;

        protected virtual void onNameChanged(EventArgs e)
        {
            if (NameChanged != null)
            {
                NameChanged(this, e);
            }
        }
    }
}
